import { Prisma, User } from '@prisma/client';
import prisma from '../lib/prisma';

export interface UserCreateInput extends Prisma.UserCreateInput {}
export interface UserUpdateInput extends Prisma.UserUpdateInput {}
export interface UserQueryOptions {
  page?: number;
  limit?: number;
}

export class UserService {
  static async findMany(options: UserQueryOptions): Promise<{ users: User[]; total: number }> {
    const { page = 1, limit = 10 } = options;
    const skip = (page - 1) * limit;

    const [users, total] = await Promise.all([
      prisma.user.findMany({
        skip,
        take: limit,
        orderBy: { createdAt: 'desc' },
      }),
      prisma.user.count(),
    ]);

    return { users, total };
  }

  static async findById(id: number): Promise<User | null> {
    return prisma.user.findUnique({
      where: { id },
    });
  }

  static async create(data: UserCreateInput): Promise<User> {
    return prisma.user.create({
      data,
    });
  }

  static async update(id: number, data: UserUpdateInput): Promise<User> {
    return prisma.user.update({
      where: { id },
      data,
    });
  }

  static async delete(id: number): Promise<void> {
    await prisma.user.delete({
      where: { id },
    });
  }
}
